package com.example.m2l_digicode;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

public class Wifi extends Activity {

	
	String wifi = null;
    ResultSet result;
    PreparedStatement statement;
    Handler handler = null;

    ProgressDialog loadingdialog;
    
    String mois="1";
    
    String requete =null;
    AlertDialog affiche_wifi;
    java.sql.Connection conn = null;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wifi);
		// Show the Up button in the action bar.
		
		//getActionBar().setDisplayHomeAsUpEnabled(true);
		
		
		//********************** ONTENTION DU MOIS EN COURS******************
		 Calendar calendrier = Calendar.getInstance();
		 int mois_actuel = calendrier.get(Calendar.MONTH);
	
		
		//***************LISTE DEROULANTE*****************************************	
		 
	    //*********************MOIS**************************
	    Spinner spin_mois = (Spinner) findViewById(R.id.liste_mois);
	    
		ArrayAdapter<CharSequence> adapter_mois = ArrayAdapter.createFromResource(
		this, R.array.tab_mois, android.R.layout.simple_spinner_item);
		spin_mois.setAdapter(adapter_mois);
		 
	    //Ajout Listener sur sélection dans la liste
	    spin_mois.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
	        @Override
	        public void onItemSelected(AdapterView<?> parent, View view,
	                int pos, long id) {
	        	 mois=String.valueOf(pos+1);
	        	 
	        	 
	        	 Log.i("num_mois", mois);
	        }
	        @Override
	        public void onNothingSelected(AdapterView<?> arg0) {

	        }
	    });
	
	    spin_mois.setSelection(mois_actuel);

        final Button button_wifi = (Button) findViewById(R.id.button_wifi);
        button_wifi.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	get_wifi();
            }  
        });
	}


	public void get_wifi()
	{
	    
		new Thread(new Runnable() {
	        @Override
	        public void run() {
	        	requete = "SELECT CODE FROM WIFI WHERE NUM_MOIS="+mois;
	        	
                runOnUiThread(new Runnable() {	                   
					public void run() {
						loadingdialog = ProgressDialog.show(Wifi.this, "","Chargement en cours...",true);
   
                   }
               });
	        	
			
		       try
		       {
		           
		           try {
		               Class.forName("com.mysql.jdbc.Driver").newInstance();
		        
		           } catch (InstantiationException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Instanciation Driver", e.getMessage());
		           } catch (IllegalAccessException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Access Exception", e.getMessage());

		           } catch (ClassNotFoundException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Class", e.getMessage());
		           }
		                    
		            
		           String url = "jdbc:mysql://192.168.1.26/digicode?autoReconnect=true";
		           //String url = "jdbc:mysql://ppe-fredi.suroot.com/digicode?autoReconnect=true";
		           String user = "util";
		           String passwd = "util123";
		            
		          
		           conn = (java.sql.Connection) DriverManager.getConnection(url, user, passwd);
		               Log.i("Connection", "Etablished");
		          
		               statement=(PreparedStatement) conn.prepareStatement(requete);
		               result=(ResultSet) statement.executeQuery();
		               
		                while(result.next())
		                {
		                	wifi=result.getString(1);
		                }
		                
		                runOnUiThread(new Runnable() {
		                    @SuppressWarnings("deprecation")
							public void run() {

		                    	loadingdialog.dismiss();
		                        
		                    	affiche_wifi = new AlertDialog.Builder(Wifi.this).create();
		                        affiche_wifi.setTitle("Clef WIFI");
		                        affiche_wifi.setMessage(wifi);     
		                        affiche_wifi.setButton("OK", new DialogInterface.OnClickListener() {
		                            public void onClick(DialogInterface dialog, int which) {
		                            	affiche_wifi.dismiss();
		                          } });
		                        affiche_wifi.show();
		                   }
		               });
	
		       }
		       
		       catch(SQLException e)
		       {
		           e.printStackTrace();
		           Log.e("MySQL", e.getMessage());
		       }
			  
	        }
	       
	    }).start();    
	}
	
}
